//新增工作经历事件
function addHistory(){
    $("#historyShow").hide();
    $("#vitaeIsNot").hide();
    $("#addVitae").show();
    $("#saveHistory").show();
    $("#saveVitae").hide();

}

//取消新增工作经历
function closeHistory(){
    $("#historyShow").show();
    $("#vitaeIsNot").show();
    $("#addVitae").hide();
    $("#saveHistory").hide();
    $("#saveVitae").show();
}

layui.use('laydate', function(){
    var laydate = layui.laydate;

    //出生年月
    laydate.render({
        elem: '#birthday',
        format:"yyyy-MM-dd"
    });

    //毕业时间
    laydate.render({
        elem: '#finishDate',
        format:"yyyy-MM-dd"
    });

    //在职时间
    laydate.render({
        elem: '#workDate'
        ,type: 'month'
        ,range: '至'
    });

    if(jobProvinceId > 0){
        setChildAreas(jobProvinceId, 'jobCityId');
        $('#jobProvinceId').val(jobProvinceId);
    }
    if(jobCityId > 0){
        $('#jobCityId').val(jobCityId);
    }
})

//选中性别事件
function selectSex(num, obj) {
    $(obj).parent().children().removeClass("current");
    $(obj).addClass("current");
    $("#sex").val(num);

}

//选中求职类型事件
function selectJobType(num, obj) {
    $(obj).parent().children().removeClass("current");
    $(obj).addClass("current");
    $("#jobType").val(num);
}

//选中学习制度类型事件
function selectLearningType(num, obj) {
    $(obj).parent().children().removeClass("current");
    $(obj).addClass("current");
    $("#learningType").val(num);
}

//选择省份事件
function provinceClick() {
    var provinceId = $("#jobProvinceId").val();
    $('#jobCityId option').each(function () {
        if ($(this).val() !== "0") $(this).remove();
    });
    setChildAreas(provinceId, 'jobCityId');
};

//城市连动事件
function setChildAreas(pid, objName) {
    if (pid === "0") return;
    $.ajax({
        type: "GET",
        url: "/ajax/area/children.json?pid=" + pid,
        dataType: "json", async: false,
        success: function (json) {
            var data = json.data;
            var obj = $("#" + objName);
            if (data.length < 1) {
                obj.append($("<option>").val(pid).text(pid)).val(pid).addClass("select-hide");
            } else {
                for (i = 0; i < data.length; i++) {
                    obj.append($("<option>").val(data[i].id).text(data[i].name));
                }
                obj.removeClass("select-hide");
            }
        },
        error: function (data) {
            layer.msg('error:读取地区数据失败，请重试！', {icon: 2});
        }
    });
}

//保存简历
function saveVitae() {
    var params = $('#vitaeForm').serialize();
    var index = layer.load(0, {shade: false});
    $.ajax({
        url: '/user/vitae/save.json',
        type: 'post',
        dataType: 'json',
        data: params,
        success: function (json) {
            layer.close(index);
            if (json.code === 0) {
                layer.msg("保存成功", {icon: 1});
            }else {
                layer.msg(json.msg, {icon: 2});
            }
        }
    });

}

//简历下载
function vitaeDownload() {
    $.get("/user/vitae/download.json", function (json) {
        if (json.code === 0) {
            var url = json.data;
            var name = "个人简历";
            var x = new XMLHttpRequest();
            x.open("GET", url, true);
            x.responseType = 'blob';
            x.onload = function (e) {
                var url = window.URL.createObjectURL(x.response)
                var a = document.createElement('a');
                a.href = url;
                a.download = name.indexOf(".") < 1 ? name + ".pdf" : name;
                a.click();
            }
            x.send();
        } else {
            return layer.msg(json.msg);
        }
    })
}

//保存工作经历
function saveHistory() {
    var params = $('#historyForm').serialize();
    var index = layer.load(0, {shade: false});
    $.ajax({
        url: '/user/history/save.json',
        type: 'post',
        dataType: 'json',
        data: params,
        success: function (json) {
            layer.close(index);
            if (json.code === 0) {
                layer.msg("保存成功", {icon: 1});
                $("#vitaeIsNot").remove();
                $("#historyShow").show();
                $("#addVitae").hide();
                $("#saveHistory").hide();
                $("#saveVitae").show();
                $("#historyForm :input").val("");
                var html =  '<li class="layui-timeline-item" id="history-'+json.data.id+'">\n' +
                    '<i class="layui-icon layui-timeline-axis"></i>\n' +
                    '<div class="layui-timeline-content layui-text">\n' +
                    '<h3 class="layui-timeline-title">'+json.data.workUnit+'\n' +
                    '<a href="javascript:void (0)" onclick="delHistory('+json.data.id+')" title="删除" \n' +
                    'class="layui-btn layui-btn-primary layui-btn-xs ml20" style="margin-top: -5px">删除</a>\n' +
                    '</h3>\n'+
                    '<p>\n' +
                    '<span>职位：'+json.data.position+'</span>\n' +
                    '<span class="ml50">在职时间：'+json.data.workDate+'</span>\n' +
                    '</p>\n' +
                    '<p>\n' +
                    '<span>内容：</span>\n' +
                    '<span>'+json.data.workIntro+'</span>\n' +
                    '</p>\n' +
                    '</div>\n' +
                    '</li>';
                $("#history-list").prepend(html);
            }else {
                layer.msg(json.msg, {icon: 2});
            }
        }
    });
}

//删除工作经历
function delHistory(id) {
    layer.confirm('该操作不可逆转，确定要删除？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.post("/user/history/del.json",{"id":id},function (json) {
            if(json.code === 0){
                layer.msg("删除成功", {icon: 1});
                $("#history-"+id).remove();
            }else {
                layer.msg(json.msg, {icon: 2});
            }
        })
    });
}
